package com.inei.cie.capituloi;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.inei.cie.R;
import com.inei.cie.capituloix.CapituloIX_Activity;
import com.inei.cie.dao.CapituloIDao;
import com.inei.cie.entidades.P1_B_3N;
import com.inei.cie.entidades.P1_B_3_12N;
import com.inei.cie.utilitarios.Variables;

public class CapituloI_SeccionB_Predio_Activity extends Activity {

	public Variables var;
	public CapituloIDao capitulo;
	public String id_local;
	public int Nro_Pred;
	public Bundle b;
	public P1_B_3N p1_b_3n;
	public List<P1_B_3_12N> lista;

	public TextView tvTitulo, tv1B0pregunta;

	public EditText et1B0, et1B1, et1B3, et1B4, et1B5, et1B6, et1B7;
	public EditText et1B8entero, et1B8decimal, et1B9entero, et1B9decimal;
	public EditText et1B11;
	public Spinner sp1B0, sp1B1, sp1B2, sp1B4, sp1B6, sp1B10, spUnidad1,
			spUnidad2;
	public LinearLayout ll1B12;
	public List<EditText> lista12;

	public TableLayout tlCapitulo1;
	public TableRow tr1B2, tr1B3, tr1B4, tr1B5, tr1B6, tr1B7, tr1B8;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_i_seccion_b_predio);
		IniciarComponentes();
	}

	public void IniciarComponentes() {
		var = new Variables();
		capitulo = new CapituloIDao(this);
		b = getIntent().getExtras();
		id_local = b.getString(var.ID_LOCAL);
		Nro_Pred = b.getInt(var.NRO_PRED);

		tvTitulo = (TextView) findViewById(R.id.tvTitulo);
		tvTitulo.setText("Predio Nro. "
				+ (Nro_Pred < 10 ? "0" + Nro_Pred : "" + Nro_Pred));

		tv1B0pregunta = (TextView) findViewById(R.id.tv1B0pregunta);
		tv1B0pregunta
				.setText(tv1B0pregunta
						.getText()
						.toString()
						.replace(
								"XX",
								(Nro_Pred < 10 ? "0" + Nro_Pred : "" + Nro_Pred)));

		tlCapitulo1 = (TableLayout) findViewById(R.id.tlCapitulo1);
		tr1B2 = (TableRow) findViewById(R.id.tr1B2);
		tr1B3 = (TableRow) findViewById(R.id.tr1B3);
		tr1B4 = (TableRow) findViewById(R.id.tr1B4);
		tr1B5 = (TableRow) findViewById(R.id.tr1B5);
		tr1B6 = (TableRow) findViewById(R.id.tr1B6);
		tr1B7 = (TableRow) findViewById(R.id.tr1B7);
		tr1B8 = (TableRow) findViewById(R.id.tr1B8);

		spUnidad1 = (Spinner) findViewById(R.id.spUnidad1);
		spUnidad2 = (Spinner) findViewById(R.id.spUnidad2);

		spUnidad1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				et1B8entero.requestFocus();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		spUnidad2.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				et1B9entero.requestFocus();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		sp1B0 = (Spinner) findViewById(R.id.sp1B0);
		sp1B0.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View v, int pos,
					long arg3) {
				if (pos == 0) {
					et1B0.setVisibility(View.VISIBLE);
				} else {
					et1B0.setVisibility(View.GONE);
					et1B0.setText("");
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		et1B0 = (EditText) findViewById(R.id.et1B0);
		sp1B1 = (Spinner) findViewById(R.id.sp1B1);
		sp1B1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				if (pos == 4 || pos == 5) {
					et1B1.setVisibility(View.VISIBLE);
					et1B1.requestFocus();
				} else {
					et1B1.setVisibility(View.GONE);
					et1B1.setText("");
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		et1B1 = (EditText) findViewById(R.id.et1B1);
		sp1B2 = (Spinner) findViewById(R.id.sp1B2);
		sp1B2.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View v, int pos,
					long arg3) {
				if (pos == 5) {
					tr1B3.setVisibility(View.GONE);
					et1B3.setText("");
				} else {
					tr1B3.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		et1B3 = (EditText) findViewById(R.id.et1B3);
		et1B3.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence texto, int arg1, int arg2,
					int arg3) {
				if (texto.toString().trim().length() != 0) {
					tr1B4.setVisibility(View.GONE);
					sp1B4.setSelection(0);
					et1B4.setText("");
					tr1B5.setVisibility(View.GONE);
					et1B5.setText("");
					tr1B6.setVisibility(View.GONE);
					sp1B6.setSelection(0);
					et1B6.setText("");
					tr1B7.setVisibility(View.GONE);
					et1B7.setText("");
				} else {
					tr1B4.setVisibility(View.VISIBLE);
					tr1B5.setVisibility(View.VISIBLE);
					tr1B6.setVisibility(View.VISIBLE);
					tr1B7.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
		sp1B4 = (Spinner) findViewById(R.id.sp1B4);
		sp1B4.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View v, int pos,
					long arg3) {
				if (pos == 10) {
					tr1B5.setVisibility(View.GONE);
					et1B5.setText("");
					et1B4.setVisibility(View.GONE);
					et1B4.setText("");
				} else {
					tr1B5.setVisibility(View.VISIBLE);
					if (pos == 9) {
						et1B4.setVisibility(View.VISIBLE);
					} else {
						et1B4.setVisibility(View.GONE);
						et1B4.setText("");
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		et1B4 = (EditText) findViewById(R.id.et1B4);
		et1B5 = (EditText) findViewById(R.id.et1B5);
		et1B5.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence texto, int arg1, int arg2,
					int arg3) {
				if (texto.toString().trim().length() != 0) {
					tr1B6.setVisibility(View.GONE);
					sp1B6.setSelection(0);
					et1B6.setText("");
					tr1B7.setVisibility(View.GONE);
					et1B7.setText("");
				} else {
					tr1B6.setVisibility(View.VISIBLE);
					tr1B7.setVisibility(View.VISIBLE);
				}

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
		et1B5.setFocusable(false);

		et1B5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText et = (EditText) v;
				AbrirCalendar(et.getText().toString(), et);

			}
		});
		sp1B6 = (Spinner) findViewById(R.id.sp1B6);
		sp1B6.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View v, int pos,
					long arg3) {
				if (pos == 10) {
					tr1B7.setVisibility(View.GONE);
					et1B7.setText("");
					et1B6.setVisibility(View.GONE);
					et1B6.setText("");
				} else {
					tr1B7.setVisibility(View.VISIBLE);
					if (pos == 9) {
						et1B6.setVisibility(View.VISIBLE);
						et1B6.requestFocus();
					} else {
						et1B6.setVisibility(View.GONE);
						et1B6.setText("");
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		et1B6 = (EditText) findViewById(R.id.et1B6);
		et1B7 = (EditText) findViewById(R.id.et1B7);
		et1B7.setFocusable(false);
		et1B7.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText et = (EditText) v;
				AbrirCalendar(et.getText().toString(), et);

			}
		});

		et1B8entero = (EditText) findViewById(R.id.et1B8entero);
		et1B8entero.setNextFocusDownId(R.id.et1B8decimal);

		et1B8decimal = (EditText) findViewById(R.id.et1B8decimal);
		et1B8decimal.setNextFocusDownId(R.id.et1B9entero);

		et1B9entero = (EditText) findViewById(R.id.et1B9entero);
		et1B9entero.setNextFocusDownId(R.id.et1B9decimal);

		et1B9decimal = (EditText) findViewById(R.id.et1B9decimal);
		sp1B10 = (Spinner) findViewById(R.id.sp1B10);
		sp1B10.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View v, int pos,
					long arg3) {
				if (pos != 1) {
					et1B11.setText("");
					et1B11.setEnabled(false);
				} else {
					et1B11.setEnabled(true);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		et1B11 = (EditText) findViewById(R.id.et1B11);
		et1B11.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (!s.toString().equals("")) {
					if (Integer.parseInt(s.toString()) > 10) {
						et1B11.setText("");
						Toast.makeText(
								CapituloI_SeccionB_Predio_Activity.this,
								"El n\u00famero de locales escolares, otras instituciones o serviciones con las que se comparte el predio no debe ser mayor a 10",
								Toast.LENGTH_LONG).show();
						et1B11.requestFocus();
					} else {
						if (s.toString().equals("") || s.toString().equals("0")) {
							ll1B12.removeAllViewsInLayout();
							lista12.clear();
						} else {
							ll1B12.removeAllViewsInLayout();
							lista12 = new ArrayList<EditText>();
							int cant = Integer.parseInt(s.toString());
							for (int i = 0; i < cant; i++) {
								Agregar12("");
							}
						}
					}
				}

				if (s.toString().equals("") || s.toString().equals("0")) {
					ll1B12.removeAllViewsInLayout();
					lista12.clear();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
		et1B11.setEnabled(false);
		ll1B12 = (LinearLayout) findViewById(R.id.ll1B12);
		lista12 = new ArrayList<EditText>();
		CargarData();
	}

	public void Agregar12(String nomb) {
		EditText et = new EditText(this);
		et.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
		et.setText(nomb);
		ll1B12.addView(et);
		lista12.add(et);
	}

	public boolean Validar12() {
		if (lista == null) {
			return false;
		}
		for (int i = 0; i < lista.size(); i++) {
			if (lista.get(i).getP1_B_3_12_NombComp().trim().length() == 0) {
				lista12.get(i).requestFocus();
				return true;
			}
		}
		return false;
	}

	public void CargarData() {
		p1_b_3n = capitulo.ObtenerPredio(id_local, Nro_Pred);
		if (p1_b_3n != null) {
			sp1B0.setSelection(p1_b_3n.getP1_B_3_InmTip());
			if (p1_b_3n.getP1_B_3_InmCod() != null) {
				et1B0.setText(p1_b_3n.getP1_B_3_InmCod());
			}
			sp1B1.setSelection(p1_b_3n.getP1_B_3_1_Prop());
			if (p1_b_3n.getP1_B_3_1_Prop_O() != null) {
				et1B1.setText(p1_b_3n.getP1_B_3_1_Prop_O());
			}

			sp1B10.setSelection(p1_b_3n.getP1_B_3_10_Comp());
			et1B11.setText(p1_b_3n.getP1_B_3_11_CompCan() + "");

			if (p1_b_3n.getP1_B_3_9_At_Local() != 0) {
				spUnidad1.setSelection(p1_b_3n.getP1_B_3_9_Tip_At_Local());
				String[] num2 = (p1_b_3n.getP1_B_3_9_At_Local() + "")
						.split("\\.");
				et1B9entero.setText(num2[0] + "");
			}
			if (p1_b_3n.getP1_B_3_8_At_Pred() != 0) {
				spUnidad2.setSelection(p1_b_3n.getP1_B_3_8_Tip_At_Pred());
				String[] num1 = (p1_b_3n.getP1_B_3_8_At_Pred() + "")
						.split("\\.");
				et1B8entero.setText(num1[0] + "");
			}

			et1B7.setText(p1_b_3n.getP1_B_3_7_DocPos_Fech());

			sp1B6.setSelection(p1_b_3n.getP1_B_3_6_DocPos());
			if (p1_b_3n.getP1_B_3_6_DocPos_O() != null) {
				et1B6.setText(p1_b_3n.getP1_B_3_6_DocPos_O().toUpperCase());
			}
			et1B5.setText(p1_b_3n.getP1_B_3_5_FecTit());

			sp1B4.setSelection(p1_b_3n.getP1_B_3_4_Tipo_TProp());

			if (p1_b_3n.getP1_B_3_4_Tipo_TProp_O() != null) {
				et1B4.setText(p1_b_3n.getP1_B_3_4_Tipo_TProp_O().toUpperCase());
			}

			if (p1_b_3n.getP1_B_3_3_AntReg_Nro() != null) {
				et1B3.setText(p1_b_3n.getP1_B_3_3_AntReg_Nro().toUpperCase());
			}

			sp1B2.setSelection(p1_b_3n.getP1_B_3_2_AntReg_Cod());

			lista = capitulo.ObtenerPredio12(p1_b_3n);
			if (lista != null && lista12 != null) {
				for (int i = 0; i < lista.size(); i++) {
					if (i < lista12.size()) {
						if (lista12.get(i) != null) {
							if (lista.get(i).getP1_B_3_12_NombComp() != null) {
								lista12.get(i).setText(
										lista.get(i).getP1_B_3_12_NombComp());
							}
						}
					}
				}
			}
		} else {
			p1_b_3n = new P1_B_3N();
			p1_b_3n.setId_local(id_local);
			p1_b_3n.setNro_Pred(Nro_Pred);
		}
	}

	@Override
	public void onBackPressed() {
		b.putInt(var.NRO_PRED, 1);
		Intent irPredios = new Intent(this,
				CapituloI_SeccionB_Lista_Predios_Activity.class);
		irPredios.putExtras(b);
		startActivity(irPredios);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_guardar_calc, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir ?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnFoto:
			Intent foto = new Intent(CapituloI_SeccionB_Predio_Activity.this,
					CapituloIX_Activity.class);
			b.putBoolean("pregunta", true);
			foto.putExtras(b);
			startActivity(foto);
			break;
		case R.id.mnGuardar:
			GuardarData();
			break;

		case R.id.mnCalculadora:
			Intent i = new Intent();
			i.setClassName("com.android.calculator2",
					"com.android.calculator2.Calculator");
			startActivity(i);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public boolean GuardarData() {
		if (!et1B11.getText().toString().equals("")
				&& !et1B11.getText().toString().equals("0")) {
			lista = new ArrayList<P1_B_3_12N>();
			for (int i = 0; i < lista12.size(); i++) {
				P1_B_3_12N obj = new P1_B_3_12N();
				obj.setId_local(p1_b_3n.getId_local());
				obj.setP1_B_3_NroPred(p1_b_3n.getNro_Pred());
				obj.setP1_B_3_12_Nro(i + 1);
				obj.setP1_B_3_12_NombComp(lista12.get(i).getText().toString()
						.toUpperCase());
				obj.setSwCompleto(1);
				lista.add(obj);
			}
		} else {
			lista = null;
		}
		if (sp1B0.getSelectedItemPosition() == 0
				&& et1B0.getText().toString().length() < 6) {
			Toast.makeText(
					this,
					"El C\u00f3digo del Inmueble debe tener un m\u00ednimo de 6 d\u00edgitos",
					Toast.LENGTH_SHORT).show();
			return false;
		} else if (et1B8entero.getText().toString().equals("")) {
			Toast.makeText(this,
					"Ingrese el \u00e1rea del terreno que ocupa el predio",
					Toast.LENGTH_SHORT).show();
			et1B8entero.requestFocus();
			return false;
		} else if (et1B9entero.getText().toString().equals("")) {
			Toast.makeText(
					this,
					"Ingrese el \u00e1rea del terreno que ocupa el local escolar en este predio",
					Toast.LENGTH_SHORT).show();
			et1B9entero.requestFocus();
			return false;
		} /*else if (Integer.parseInt(et1B8entero.getText().toString()) < Integer
				.parseInt(et1B9entero.getText().toString())) {
			Toast.makeText(
					this,
					"El \u00e1rea del terreno que ocupa el predio debe ser mayor o igual al \u00e1rea del terreno que ocupa el local escolar en este predio",
					Toast.LENGTH_LONG).show();
			et1B9entero.requestFocus();
			return false;
		}*/
		if (sp1B10.getSelectedItemPosition() == 1
				&& (et1B11.getText().toString().trim().equals("") || Integer
						.parseInt(et1B11.getText().toString().trim()) == 0)) {
			Toast.makeText(
					this,
					"Porfavor ingrese una cantidad mayor a 0 en la pregunta 11",
					Toast.LENGTH_LONG).show();
			et1B11.requestFocus();
			return false;
		} else if (Validar12()) {
			Toast.makeText(
					this,
					"Debe completar los " + lista.size()
							+ " nombres solicitados en la pregunta 12",
					Toast.LENGTH_LONG).show();
			return false;
		} else {

			p1_b_3n.setP1_B_3_InmTip(sp1B0.getSelectedItemPosition());
			p1_b_3n.setP1_B_3_InmCod(et1B0.getText().toString().toUpperCase());
			p1_b_3n.setP1_B_3_1_Prop(sp1B1.getSelectedItemPosition());
			p1_b_3n.setP1_B_3_1_Prop_O(et1B1.getText().toString().toUpperCase());
			p1_b_3n.setP1_B_3_2_AntReg_Cod(sp1B2.getSelectedItemPosition());
			p1_b_3n.setP1_B_3_3_AntReg_Nro(et1B3.getText().toString()
					.toUpperCase());
			p1_b_3n.setP1_B_3_4_Tipo_TProp(sp1B4.getSelectedItemPosition());
			p1_b_3n.setP1_B_3_4_Tipo_TProp_O(et1B4.getText().toString()
					.toUpperCase());
			p1_b_3n.setP1_B_3_5_FecTit(et1B5.getText().toString());
			p1_b_3n.setP1_B_3_6_DocPos(sp1B6.getSelectedItemPosition());
			p1_b_3n.setP1_B_3_6_DocPos_O(et1B6.getText().toString()
					.toUpperCase());
			p1_b_3n.setP1_B_3_7_DocPos_Fech(et1B7.getText().toString());
			if (!et1B8entero.getText().toString().equals("")
					|| !et1B8decimal.getText().toString().equals("")) {
				String num1 = (et1B8entero.getText().toString().equals("") ? 0
						: et1B8entero.getText().toString())
						+ "."
						+ (et1B8decimal.getText().toString().equals("") ? 0
								: et1B8decimal.getText().toString());
				p1_b_3n.setP1_B_3_8_Tip_At_Pred(spUnidad1
						.getSelectedItemPosition());
				p1_b_3n.setP1_B_3_8_At_Pred(Float.parseFloat(num1));
			}
			if (!et1B9entero.getText().toString().equals("")
					|| !et1B9decimal.getText().toString().equals("")) {
				String num2 = (et1B9entero.getText().toString().equals("") ? 0
						: et1B9entero.getText().toString())
						+ "."
						+ (et1B9decimal.getText().toString().equals("") ? 0
								: et1B9decimal.getText().toString());
				p1_b_3n.setP1_B_3_9_Tip_At_Local(spUnidad2
						.getSelectedItemPosition());
				p1_b_3n.setP1_B_3_9_At_Local(Float.parseFloat(num2));
			}
			p1_b_3n.setP1_B_3_10_Comp(sp1B10.getSelectedItemPosition());
			if (!et1B11.getText().toString().equals("")) {
				p1_b_3n.setP1_B_3_11_CompCan(Integer.parseInt(et1B11.getText()
						.toString()));
			}
			if (!et1B11.getText().toString().equals("")
					&& !et1B11.getText().toString().equals("0")) {
				lista = new ArrayList<P1_B_3_12N>();
				for (int i = 0; i < lista12.size(); i++) {
					P1_B_3_12N obj = new P1_B_3_12N();
					obj.setId_local(p1_b_3n.getId_local());
					obj.setP1_B_3_NroPred(p1_b_3n.getNro_Pred());
					obj.setP1_B_3_12_Nro(i + 1);
					obj.setP1_B_3_12_NombComp(lista12.get(i).getText()
							.toString().toUpperCase());
					obj.setSwCompleto(1);
					lista.add(obj);
				}
			} else {
				lista = null;
			}
			p1_b_3n.setSwCompleto(1);
			if (capitulo.GuardarPredio(p1_b_3n, lista)) {
				Toast.makeText(this, "Se guardo con \u00e9xito",
						Toast.LENGTH_SHORT).show();

				Intent irLista = new Intent(
						CapituloI_SeccionB_Predio_Activity.this,
						CapituloI_SeccionB_Lista_Predios_Activity.class);
				irLista.putExtras(getIntent().getExtras());
				startActivity(irLista);

				return true;
			} else {
				Toast.makeText(this, "No se pudo guardar", Toast.LENGTH_LONG)
						.show();
				return false;
			}
		}
	}

	public void AbrirCalendar(String f, final EditText et) {

		final Calendar c = Calendar.getInstance();
		int dia = c.get(Calendar.DAY_OF_MONTH);
		int mes = c.get(Calendar.MONTH);
		int anio = c.get(Calendar.YEAR);
		if (f.length() != 0) {
			String[] valores = f.split("/");
			dia = Integer.parseInt(valores[0]);
			mes = Integer.parseInt(valores[1]) - 1;
			anio = Integer.parseInt(valores[2]);
		}

		DatePickerDialog fecha = new DatePickerDialog(this,
				new OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						c.set(Calendar.YEAR, year);
						c.set(Calendar.MONTH, monthOfYear);
						c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
						SimpleDateFormat sdf = new SimpleDateFormat(
								"dd/MM/yyyy", Locale.US);
						if (!CompararFechasMenos(
								sdf.format(Calendar.getInstance().getTime()),
								sdf.format(c.getTime()))) {
							Toast.makeText(
									CapituloI_SeccionB_Predio_Activity.this,
									"La fecha ingresada no puede ser mayor a la fecha actual",
									Toast.LENGTH_SHORT).show();
						} else {
							et.setText(sdf.format(c.getTime()));
						}
					}
				}, anio, mes, dia);
		fecha.setButton(DatePickerDialog.BUTTON_NEUTRAL, "Borrar",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						et.setText("");
					}
				});

		fecha.show();
	}

	public boolean CompararFechasMenos(String f1, String f2) {
		try {
			String[] fecha1 = f1.split("/");
			String[] fecha2 = f2.split("/");
			if (fecha1[2].compareTo(fecha2[2]) < 0) {
				return false;
			} else if (fecha1[2].compareTo(fecha2[2]) == 0) {
				if (fecha1[1].compareTo(fecha2[1]) < 0) {
					return false;
				} else if (fecha1[1].compareTo(fecha2[1]) == 0) {
					if (fecha1[0].compareTo(fecha2[0]) < 0) {
						return false;
					}
				}
			}
			return true;
		} catch (Exception e) {
			Toast.makeText(this, "Error al comparar fechas", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
	}

}
